class Array
	def media
		if block_given? 
			map{|x| yield(x)}.inject{|x,y|  x+y}.to_f / size
		else
			inject{|x,y|  x+y}.to_f / size
		end
	end
	
	def varianza
		media{|x| x**2 } - media**2
	end
	
	def desviacion_estandar
		varianza() ** 0.5 # raiz cuadrada
	end
	
	def mediana
		if size % 2 == 0
			( self[ size/2-1 ].to_f + self[ size/2 ].to_f ) / 2
		else
			self[ (size-1) / 2 ]
		end
	end
end

def assert_equal( expected, result, tolerancia = 0 )
	
	if (expected - result).abs > tolerancia
		raise RuntimeError.new("Assertion fail. expected: #{expected}, result: #{result}")
	end
end

def casenum
	unless @casenum
		@casenum = 0
	end
	@casenum = @casenum + 1
	@casenum
end

def test_tp0( args, input, expected_output, tolerancia = 0)
	
	shell_cmd = "echo #{input} | ./tp0 #{args}"
	
	output_result  = `#{shell_cmd}`
	
	result_value = output_result.to_f
	expected_value = expected_output.to_f
	
	assert_equal expected_value, result_value, tolerancia

	print "\\subsection{Caso #{casenum} }\n"
	print "\\subsubsection{Comando}\n"
	print "\\begin{verbatim}\n"
	print "#{shell_cmd}\n"
	print "\\end{verbatim}\n"
	print "\\subsubsection{Salida}\n"
	print "\\begin{verbatim}\n"
	print "#{output_result}\n"
	print "\\end{verbatim}\n"

#	print "-"*30 + " cmd " + "-"*30,"\n"
#	print "./tp0 #{args}\n"

#	print "-"*30 + " input " + "-"*30,"\n"
#	print input,"\n"

#	print "-"*30 + " output " + "-"*30,"\n"
#	print output_result,"\n"

end


def test_tp0_media( *values )
	test_tp0("-c e", values.join(" "), values.media)
end

def test_tp0_std( *values )
	test_tp0("-c s", values.join(" "), values.desviacion_estandar, 0.001)
end

def test_tp0_mediana(*values)
	test_tp0("-c m", values.join(" "), values.mediana, 0.001)
end
